package com.xuqm.sdhbwfu.app.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xuqm.base.di.manager.HttpManager
import com.xuqm.base.extensions.showMessage
import com.xuqm.sdhbwfu.app.data.RiskCreateData
import com.xuqm.sdhbwfu.app.model.*
import com.xuqm.sdhbwfu.app.repository.Service
import com.xuqm.sdhbwfu.core.extensions.io_main
import com.xuqm.sdhbwfu.core.extensions.subscribeBy
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class RiskCreateViewModel : BaseViewModel() {

    private val _listLocation = MutableLiveData<List<RiskLoacationModel>>()
    val listLocation: LiveData<List<RiskLoacationModel>> = _listLocation

    fun locationList() {
        HttpManager.getApi(Service::class.java)
            .locationList()
            .io_main()
            .subscribeBy(
                {
                    _listLocation.postValue(it)
                }, {
                    _listLocation.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _listPure = MutableLiveData<List<RiskPureModel>>()
    val listPure: LiveData<List<RiskPureModel>> = _listPure

    fun pureList() {
        HttpManager.getApi(Service::class.java)
            .pureList()
            .io_main()
            .subscribeBy(
                {
                    _listPure.postValue(it)
                }, {
                    _listPure.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _listSchema = MutableLiveData<List<RiskSchemaModel>>()
    val listSchema: LiveData<List<RiskSchemaModel>> = _listSchema

    fun schemaList(schemadef: String, s_location: String) {
        HttpManager.getApi(Service::class.java)
            .schemaList(schemadef, s_location)
            .io_main()
            .subscribeBy(
                {
                    _listSchema.postValue(it)
                }, {
                    _listSchema.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _listUser = MutableLiveData<List<RiskUserModel>>()
    val listUser: LiveData<List<RiskUserModel>> = _listUser

    fun userList() {
        HttpManager.getApi(Service::class.java)
            .userList()
            .io_main()
            .subscribeBy(
                {
                    _listUser.postValue(it)
                }, {
                    _listUser.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _statusApply = MutableLiveData<RiskCreateModel>()
    val statusApply: LiveData<RiskCreateModel> = _statusApply
    fun applyForm(data: RiskCreateData) {
        HttpManager.getApi(Service::class.java)
            .applyForm(data)
            .io_main()
            .subscribeBy(
                {
                    _statusApply.postValue(it)
                }, {
                    _statusApply.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }


    private val _status = MutableLiveData<RiskUploadModel>()
    val status: LiveData<RiskUploadModel> = _status
    fun picUpload(logFile: File) {
        val fileBody: RequestBody =
            RequestBody.create(MediaType.parse("application/octet-stream"), logFile)
        val body = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", logFile.name, fileBody)
            .build()

        HttpManager.getApi(Service::class.java)
            .annex(body)
            .io_main()
            .subscribeBy(
                {
                    _status.postValue(it)
                }, {
                    _status.postValue(null)
                    showMessage("上传图片失败::$it")
                }
            ).adds()
    }

}